package month1;

public class DiameterOfBinaryTree543_05 {
    public static void main(String[] args) {
        System.out.println(diameterOfBinaryTree(new TreeNode(new Integer[]{1, 2, 3, 4, 5})));
    }

    static int ret;

    static int diameterOfBinaryTree(TreeNode root) {
        ret = 0;
        dfs(root);
        return ret;
    }

    static int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = dfs(root.left), right = dfs(root.right);
        ret = Math.max(ret, left + right);
        return Math.max(left, right) + 1;
    }
}
